/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cronos.agent;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        OutputMessage.println("### Cronos Agent v.0.1");
        /* Information by qosdbc-agent.properties */
        String fileProperties = System.getProperty("user.dir") + System.getProperty("file.separator") + "cronos-agent.properties";

        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(fileProperties));
        } catch (FileNotFoundException ex) {
            properties = null;
            OutputMessage.println("The properties file was not found, using information passed by argument");
        } catch (IOException ex) {
            properties = null;
            OutputMessage.println("The properties file was not found, using information passed by argument");
        }

        String localPortParam = null;
        String timeParam = null;
        String catalogHostParam = null;
        String catalogPortParam = null;
        String catalogUserParam = null;
        String catalogPasswordParam = null;
        
        if (properties != null) {
            localPortParam = properties.getProperty("local_port");
            if (localPortParam == null || localPortParam.trim().length() == 0) {
                OutputMessage.println("ERROR: Error in the value of local_port parameter");
                System.exit(0);
            }
            timeParam = properties.getProperty("time_delay");
            if (timeParam == null || timeParam.trim().length() == 0) {
                OutputMessage.println("ERROR: Error in the value of time_delay parameter");
                System.exit(0);
            }

            catalogHostParam = properties.getProperty("catalog_host");
            if (catalogHostParam == null || catalogHostParam.trim().length() == 0) {
                OutputMessage.println("ERROR: Error in the value of catalog_host parameter");
                System.exit(0);
            }
            catalogPortParam = properties.getProperty("catalog_port");
            if (catalogPortParam == null || catalogPortParam.trim().length() == 0) {
                OutputMessage.println("ERROR: Error in the value of catalog_port parameter");
                System.exit(0);
            }
            catalogUserParam = properties.getProperty("catalog_user");
            if (catalogUserParam == null || catalogUserParam.trim().length() == 0) {
                OutputMessage.println("ERROR: Error in the value of catalog_user parameter");
                System.exit(0);
            }
            catalogPasswordParam = properties.getProperty("catalog_password");
            if (catalogPasswordParam == null || catalogPasswordParam.trim().length() == 0) {
                OutputMessage.println("ERROR: Error in the value of catalog_password parameter");
                System.exit(0);
            }
        }

        if (properties == null && (args == null || args.length != 6)) {
            OutputMessage.println("Command Sintax..: java -jar cronos-agent.jar <local_port> <time_delay> <catalog_host> <catalog_port> <catalog_user> <catalog_password>");
            OutputMessage.println("Command Example.: java -jar cronos-agent.jar 7778 1000 cronos.catalog 5432 cronos_user cronos_password");
        } else {
            if (properties == null) {
                localPortParam = args[0];
                timeParam = args[1];
                catalogHostParam = args[2];
                catalogPortParam = args[3];
                catalogUserParam = args[4];
                catalogPasswordParam = args[5];
            }

            OutputMessage.println("Parameters: ");
            OutputMessage.println("Local Port...............: " + localPortParam);
            OutputMessage.println("Time (millis)............: " + timeParam);

            OutputMessage.println("Catalog Host.............: " + catalogHostParam);
            OutputMessage.println("Catalog Port.............: " + catalogPortParam);
            OutputMessage.println("Catalog User.............: " + catalogUserParam);
            OutputMessage.println("Catalog Password.........: " + catalogPasswordParam);

            int localPort = -1;
            try {
                localPort = Integer.parseInt(localPortParam);
            } catch (Exception ex) {
                OutputMessage.println("ERROR: " + localPortParam + " is not a port valid");
                System.exit(0);
            }

            long time = -1;
            try {
                time = Long.parseLong(timeParam);
            } catch (Exception ex) {
                OutputMessage.println("ERROR: " + timeParam + " is not a number valid");
                System.exit(0);
            }

            String catalogHost = catalogHostParam;

            int catalogPort = -1;
            try {
                catalogPort = Integer.parseInt(catalogPortParam);
            } catch (Exception ex) {
                OutputMessage.println("ERROR: " + catalogPortParam + " is not a port valid");
                System.exit(0);
            }

            // Creates the connection with the Catalog's Service
            Connection catalogConnection = null;
            try {
                Class.forName("org.postgresql.Driver");
                catalogConnection = DriverManager.getConnection("jdbc:postgresql://" + catalogHost + ":" + catalogPort + "/cronos", catalogUserParam, catalogPasswordParam);
                OutputMessage.println("cronos-agent: " + "connected to database");
            } catch (SQLException ex) {
                OutputMessage.println("ERROR: " + ex.getMessage());
                System.exit(0);
            } catch (ClassNotFoundException ex) {
                OutputMessage.println("ERROR: " + ex.getMessage());
                System.exit(0);
            }

            // Init the CronosMonitor
            try {
                CronosMonitor monitor = new CronosMonitor(time, catalogConnection);
                monitor.start();
            } catch (SQLException ex) {
                OutputMessage.println("ERROR: " + ex.getMessage());
                System.exit(0);
            }

            Runtime.getRuntime().addShutdownHook(new InterruptProxy(catalogConnection));
        }
    }

}
